package com.jpatest.demo.controller;

import com.jpatest.demo.Service.UserServiceimpl;
import com.jpatest.demo.entity.User;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

@Controller
@RequestMapping(value = "user")
public class UserController {

    private Logger log = LoggerFactory.getLogger(this.getClass());

    @Autowired
    UserServiceimpl userServiceimpl;

    @RequestMapping(value = "/addUser",method = RequestMethod.POST)
    public@ResponseBody
    User addUser(@RequestBody User user){
        User newUser = userServiceimpl.create(user);
        if(newUser!=null){
            System.out.println("插入数据成功！！！");
        }else{
            System.out.println("插入数据失败！！！");
        }
        return newUser;
    }

    @RequestMapping(value = "getUserById",method = RequestMethod.POST)
    public @ResponseBody User getUserById(@RequestBody Long id){
        User user = userServiceimpl.findById(id);
        if(user!=null){
            System.out.println("获取数据成功！！！");
        }else{
            System.out.println("获取数据失败！！！");
        }
        return user;
    }

    @RequestMapping(value = "update",method = RequestMethod.POST)
    public @ResponseBody User updateUser(@RequestBody User user){
        User newuser = userServiceimpl.update(user);
        if(newuser!=null){
            System.out.println("更新数据成功！！！");
        }else{
            System.out.println("更新数据失败！！！");
        }
        return newuser;
    }

    @RequestMapping(value = "delete",method = RequestMethod.POST)
    public @ResponseBody void deleteUser(@RequestBody Long id){
        userServiceimpl.delete(id);
        System.out.println("删除数据成功！！！");
    }

}
